﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

using Lidgren.Library.Network;
using System.Threading;

using BBCommon;

namespace Server
{
    static class Program
    {

        public static Form1 MainForm;
        public static BBServer ServerBB;
        private static System.Windows.Forms.NotifyIcon notifyIconNA;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MainForm = new Form1();

            // Create Server object
            ServerBB = new BBServer();
            // Catch log events
            ServerBB.Log.LogEvent += new EventHandler<NetLogEventArgs>(Program.Log_LogEvent);
            // Catch co/deco events
            ServerBB.ClientConnected += new EventHandler<ClientConnectEventArgs>(ServerBB_ClientConnected);
            ServerBB.ClientDisconnected += new EventHandler<ClientConnectEventArgs>(ServerBB_ClientDisconnected);

            Application.Idle += new EventHandler(ApplicationLoop);

            CreateNotificationIcon();

            Application.Run(MainForm);

            // shutdown; sends disconnect to all connected clients with this reason string
            ServerBB.Stop("Application exiting");

            if (notifyIconNA != null)
                notifyIconNA.Visible = false;
        }

        static void ServerBB_ClientDisconnected(object sender, ClientConnectEventArgs e)
        {
            MainForm.lbUserList.Items.Remove(e.Player.Nick);
        }

        static void ServerBB_ClientConnected(object sender, ClientConnectEventArgs e)
        {
            MainForm.lbUserList.Items.Add(e.Player.Nick);
        }

        static void ApplicationLoop(object sender, EventArgs e)
        {
            while (Win32.AppStillIdle)
            {
                ServerBB.Loop();                

                Thread.Sleep(1); // don't hog the cpu
            }
        }        

        public static void Log_LogEvent(object sender, NetLogEventArgs e)
        {
            try
            {
                if (MainForm != null && !MainForm.Disposing && !MainForm.IsDisposed)
                {
                    MainForm.rtbLog.AppendText(e.Entry.What + Environment.NewLine);
                    Win32.ScrollRichTextBox(MainForm.rtbLog);
                }
            }
            catch { } // ignore disposal problems
        }

        #region Notification Area

        static void CreateNotificationIcon()
        {
            // Initialize Notification Area Icon
            notifyIconNA = new System.Windows.Forms.NotifyIcon();
            notifyIconNA.Icon = MainForm.Icon;
            notifyIconNA.Text = "BB Server";

            notifyIconNA.Visible = true;
            notifyIconNA.MouseClick += new System.Windows.Forms.MouseEventHandler(NA_MouseClick);
        }

        static void NA_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                MainForm.Show();
                MainForm.WindowState = FormWindowState.Normal;
            }
        }

        #endregion
    }
}
